/*
 * jxSwing Project (2014)
 * 
 * by Denilson Edinaldo Pinto
 * 
 * LGPL
 * Copyright (C) 2014
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
package org.jxSwing.component.spinner;

import java.awt.Color;
import java.awt.Font;
import java.util.Calendar;
import java.util.Date;

import javax.swing.JFormattedTextField;
import javax.swing.JSpinner;
import javax.swing.SpinnerDateModel;
import javax.swing.SwingConstants;
import javax.swing.UIManager;
import javax.swing.border.LineBorder;

import org.jxSwing.component.JXComponent;
import org.jxSwing.i18n.I18N;
import org.jxUtils.check.CheckUtil;

/**
 * @author Denilson Edinaldo Pinto
 * @porject jxSwing
 * @package org.jxSwing.component.spinner
 * @date 08/02/2014 - 13:47:14
 */
public class DateSpinner extends JSpinner implements JXComponent {
	
	private static final long serialVersionUID = 5943189325660558456L;
	
	private boolean atributte = true;
	
	private boolean required = false;
	
	private boolean requiredBorderEnabled = true;
	
	private int index = -1;
	
	/**
	 * 
	 */
	public DateSpinner() {
	
		super(new SpinnerDateModel());
		
		initGUI();
	}
	
	/**
	 * @param value
	 * @param start
	 * @param end
	 * @param calendarField
	 */
	public DateSpinner(final Date value, final Comparable<? extends Date> start, final Comparable<? extends Date> end, final int calendarField) {
	
		super(new SpinnerDateModel(value, start, end, calendarField));
		
		initGUI();
	}
	
	/**
	 * @param pattern
	 */
	public DateSpinner(final String pattern) {
	
		this();
		
		setDateFormat(pattern);
	}
	
	/**
	 * 
	 */
	private void initGUI() {
	
		setEditable(false);
	}
	
	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.swing.JSpinner#getEditor()
	 */
	@Override
	public DateEditor getEditor() {
	
		return (DateEditor)super.getEditor();
	}
	
	@Override
	public int getIndex() {
	
		return index;
	}
	
	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.swing.JSpinner#getModel()
	 */
	@Override
	public SpinnerDateModel getModel() {
	
		return (SpinnerDateModel)super.getModel();
	}
	
	/**
	 * @return calendar
	 */
	public Calendar getNow() {
	
		Calendar calendar = Calendar.getInstance();
		
		calendar.setTime(getValue());
		
		return calendar;
	}
	
	/**
	 * @return getTextField().getText()
	 */
	public String getText() {
	
		return getTextField().getText();
	}
	
	/**
	 * @return formattedTextField
	 */
	public JFormattedTextField getTextField() {
	
		return getEditor().getTextField();
	}
	
	/**
	 * @return getTextField().getText()
	 */
	public String getTextWithoutMask() {
	
		return getTextField().getText().replaceAll("\\D", "");
	}
	
	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.swing.JSpinner#getValue()
	 */
	@Override
	public Date getValue() {
	
		return (Date)getModel().getValue();
	}
	
	@Override
	public boolean isAtributte() {
	
		return atributte;
	}
	
	@Override
	public boolean isRequired() {
	
		return required;
	}
	
	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jxSwing.component.JXComponent#isRequiredBorderEnabled()
	 */
	@Override
	public boolean isRequiredBorderEnabled() {
	
		return requiredBorderEnabled;
	}
	
	@Override
	public void setAtributte(final boolean atributte) {
	
		this.atributte = atributte;
	}
	
	/**
	 * @param pattern
	 */
	public void setDateFormat(final String pattern) {
	
		final Font font = getTextField().getFont();
		
		setEditor(new JSpinner.DateEditor(this, pattern));
		
		getTextField().setFont(font);
		getTextField().setHorizontalAlignment(SwingConstants.CENTER);
	}
	
	/**
	 * @param b
	 */
	public void setEditable(final boolean b) {
	
		new Thread(new Runnable() {
			
			@Override
			public void run() {
			
				try {
					Thread.sleep(100);
					
				}catch(final InterruptedException interruptedException) {
					// ignore
				}
				
				getTextField().setBackground(Color.WHITE);
				getTextField().setHorizontalAlignment(SwingConstants.CENTER);
				getTextField().setEditable(b);
			}
		}).start();
	}
	
	@Override
	public void setIndex(final int index) {
	
		this.index = index;
	}
	
	/*
	 * (non-Javadoc)
	 * 
	 * @see java.awt.Component#setName(java.lang.String)
	 */
	@Override
	public void setName(final String name) {
	
		super.setName(name);
		
		setRequired(isRequired());
	}
	
	/**
	 * 
	 */
	public void setNow() {
	
		setValue(new Date());
	}
	
	@Override
	public void setRequired(final boolean required) {
	
		this.required = required;
		
		//
		if(required) {
			
			//
			if(isRequiredBorderEnabled()) {
				
				setBorder(new LineBorder(Color.RED));
			}
			
			//
			if(CheckUtil.isNullOrAbsoluteEmpty(getName())) {
				
				setToolTipText(I18N.component().requiredThis());
				
			}else {
				
				setToolTipText(I18N.component().requiredField(getName()));
			}
			
		}else {
			
			setBorder(UIManager.getBorder("Spinner.border"));
			setToolTipText(null);
		}
	}
	
	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jxSwing.component.JXComponent#setRequiredBorderEnabled(boolean)
	 */
	@Override
	public void setRequiredBorderEnabled(final boolean requiredBorderEnabled) {
	
		this.requiredBorderEnabled = requiredBorderEnabled;
	}
	
	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.swing.JSpinner#setValue(java.lang.Object)
	 */
	@Override
	public void setValue(final Object value) {
	
		super.setValue(value != null ? value : new Date());
	}
}
